/**
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.login;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.apache.http.Header;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.event.UpdateInfoEvent;
import com.tstudy.laoshibang.mode.response.BindMobileResponse;
import com.tstudy.laoshibang.mode.response.GetValidateNoResponse;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import de.greenrobot.event.EventBus;
@EFragment(R.layout.bind_mobile)
public class BindMobileFragment extends BaseFragment{
	final static String TAG = "bind_mobile";
	public BindMobileFragment() {
		super();
	}
	
	@ViewById(R.id.bind_mobile_root)
	LinearLayout mRootLayout;
	
	@ViewById(R.id.bind_password_validate_mobile)
	EditText mMobileExt;
	
	@ViewById(R.id.bind_password_validate_ext)
	EditText mValidateExt;
	
	@ViewById(R.id.bind_password_validate_get)
	Button mVerifyCodeGet;
	
	@ViewById(R.id.set_password_mobile)
	EditText mSetPwdExt;
	
	@ViewById(R.id.set_password_confirm_mobile)
	EditText mConfirmPwdExt;
	
	@ViewById(R.id.bind_mobile_validate_commit)
	TextView mCommitTew;
	
	@ColorRes(R.color.whiteColor)
	public int whiteColor;
	@ColorRes(R.color.blueColor)
	public int blueColor;
	
	String mMobile;
	String mPassword;
	String mVerifyCode;
	String mConfirmPwd;
	String mSource;
	
	int totalSecond = 60;
	int remainSecend = totalSecond;
	int mVerifyState;
	
	
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;

	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,BindMobileFragment_.class.getName(),args);
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		mRootLayout.setOnClickListener(null);
	}
	
	@Click({R.id.bind_password_validate_get,R.id.bind_mobile_back,R.id.bind_mobile_validate_commit})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.bind_mobile_back:
			backAction(mFragmentId);
			break;
		case R.id.bind_password_validate_get:
			getVerifyCode();
			break;
		case R.id.bind_mobile_validate_commit:
			bindAction();
			break;
		default:
			break;
		}
		showSoftKeyBoard(mMobileExt, false);
	}
	
	
	
	public void getVerifyCode(){
		if (TextUtils.isEmpty(mMobileExt.getText()) || !CommonUtil.checkMobile(mMobileExt.getText().toString())) {
			BaseApplication.showToast(R.string.regist_error_mobile_illegal);
			return;
		}
		mMobile = mMobileExt.getText().toString();
		BaseJsonHandler<GetValidateNoResponse> response = new BaseJsonHandler<GetValidateNoResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, GetValidateNoResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (response != null && response.getErr() == CONSTANT.NO_ERROR_STATUS) {
					if (response.data != null) {
						BaseApplication.mBindMobileSmsId = response.data.smsId;
						hideLoadingProgressDialog();
						BaseApplication.showToast("发送成功");
//						BindMobileConfirmFragment.add(mFragmentId,mMobile);
					}
//					mVerifyCodeGet.setTextColor(blueColor);
				} else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			protected GetValidateNoResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,GetValidateNoResponse.class);
			}
		};
		showProgressDialog("", "发送验证码", 0);
		HttpManager.getInstance().getValidateNo(mMobile, CONSTANT.VALIDATE_NO_TYPE_REGIST, response);
	}
	
	public void bindAction(){
		if(TextUtils.isEmpty(mMobileExt.getText()) 
				|| TextUtils.isEmpty(mValidateExt.getText()) 
				|| TextUtils.isEmpty(mSetPwdExt.getText()) 
				|| TextUtils.isEmpty(mConfirmPwdExt.getText())){
			BaseApplication.showToast(R.string.bind_error_not_null);
			return;
		}else{
			mMobile =mMobileExt.getText().toString(); 
			mVerifyCode = mValidateExt.getText().toString();
			mPassword = mSetPwdExt.getText().toString();
			mConfirmPwd = mConfirmPwdExt.getText().toString();
			if(!mPassword.equals(mConfirmPwd)) {
				BaseApplication.showToast(R.string.regist_error_password_not_confirmed);
				return;
			}
		}
		BaseJsonHandler<BindMobileResponse> response = new BaseJsonHandler<BindMobileResponse>() {
			@Override
			public void onStart() {
				showProgressDialog("", "绑定中...", 0);
				super.onStart();
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, BindMobileResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					backAction(mFragmentId);
					BaseApplication.showToast("绑定成功");
					BaseApplication.mBindMobileSmsId = null;
					BaseApplication.mCurrentUser.mobile = mMobile;
					EventBus.getDefault().post(new UpdateInfoEvent());
                } else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			protected BindMobileResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,BindMobileResponse.class);
			}
		};
		HttpManager.getInstance().bindMobile(mMobile, mVerifyCode,CONSTANT.USER_ROLE_TEACHER ,BaseApplication.mBindMobileSmsId,mPassword,response);
		
	}
	
	@Override
	public void clear() {
		if (mMobileExt != null) {
			showSoftKeyBoard(mMobileExt,false);
		}
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}

}
